package com.chisheng.service.impl;

import com.chisheng.mapper.StudentMapper;
import com.chisheng.pojo.Page;
import com.chisheng.pojo.Student;
import com.chisheng.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 学生服务默认实现
 *
 * @author chisheng
 * @since 2021-11-05
 */
@Service
public class StudentServiceImpl implements StudentService {

    /**
     * 学生 DAO
     */
    private final StudentMapper studentMapper;

    @Autowired
    public StudentServiceImpl(StudentMapper studentMapper) {
        this.studentMapper = studentMapper;
    }

    @Override
    public boolean add(Student student) {
        return studentMapper.insertOne(student) > 0;
    }

    @Override
    public boolean remove(Integer id) {
        return studentMapper.deleteById(id) > 0;
    }

    @Override
    public boolean update(Student student) {
        return studentMapper.updateById(student) > 0;
    }

    @Override
    public Student query(Integer id) {
        return studentMapper.findById(id);
    }

    @Override
    public Page<Student> queryPage(int current, int size) {
        int offset = (current - 1) * size;
        long total = studentMapper.count();
        List<Student> rows = studentMapper.findStudentLimit(offset, size);
        return new Page<>(current, size, total, rows);
    }

}
